package cn.stylefeng.guns.weixin.singlePageConfig;

import cn.stylefeng.guns.modular.about.pojo.request.AboutRequest;
import cn.stylefeng.guns.modular.about.service.AboutService;
import cn.stylefeng.guns.modular.config.entity.Config;
import cn.stylefeng.guns.modular.config.service.ConfigService;
import cn.stylefeng.guns.modular.nameNumebr.entity.NameNumber;
import cn.stylefeng.guns.modular.nameNumebr.service.NameNumberService;
import cn.stylefeng.roses.kernel.config.modular.entity.SysConfig;
import cn.stylefeng.roses.kernel.config.modular.service.SysConfigService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ApiResource(name = "单页配置")
public class SinglePageConfigApiController {
    @Resource
    ConfigService configService;
    @Resource
    AboutService aboutService;
    @Resource
    NameNumberService nameNumberService;

    @Resource
    SysConfigService sysConfigService;

    @GetResource(name = "首页统计", path = "/wx/home/tongji", requiredPermission = false, requiredLogin = false)
    public ResponseData tongji() {
        List<NameNumber> nameNumbers = this.nameNumberService.list();
        return new SuccessResponseData(nameNumbers);
    }


    @GetResource(name = "联系我们", path = "/wx/contact/detail", requiredPermission = false, requiredLogin = false)
    public ResponseData getContactUs() {
        List<Config> configs = this.configService.list();
        Map<String, String> data = new HashMap<>();
        for (Config config : configs) {
            data.put(config.getK(), config.getV());
        }
        return new SuccessResponseData(data);
    }

    /**
     * 关于我们
     *
     * @author liuxue
     * @date 2022/03/08 11:14
     */
    @GetResource(name = "关于我们", path = "/wx/about/detail", requiredPermission = false, requiredLogin = false)
    public ResponseData aboutdetail(@Validated(AboutRequest.detail.class) AboutRequest aboutRequest) {
        return new SuccessResponseData(aboutService.detail(aboutRequest));
    }

    /**
     * 获取网站关键词和描述
     *
     * @author liuxue
     * @date 2022/03/08 11:14
     */
    @GetResource(name = "获取网站关键词和描述", path = "/wx/getKeywordsAndDescription", requiredPermission = false, requiredLogin = false)
    public ResponseData getKeywordsAndDescription() {
        List<String> configCode = new ArrayList<>();
        configCode.add("baidu_keywords");
        configCode.add("baidu_description");
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysConfig::getConfigCode, configCode);
        queryWrapper.select(SysConfig::getConfigCode, SysConfig::getConfigValue);
        List<Map<String, Object>> sysConfigs = sysConfigService.listMaps(queryWrapper);
        Map<Object, Object> data = new HashMap<>();
        for (Map<String, Object> sysConfig : sysConfigs) {
            data.put(sysConfig.get("config_code"), sysConfig.get("config_value"));
        }
        return new SuccessResponseData(data);
    }
}
